package view;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.List;
import javax.swing.JFrame;


/**
 * this class could be reused, given a list of integers, it could draw a line
 * graph.
 * 
 * @author difan, minor edits by kathleen
 * 
 */

public class LineGraphDrawer extends GraphDrawer {
    private static final long serialVersionUID = 1L;
    private static final int OFFSET = 10;
  // private static final int MAX_SCORES = 87;
    private List<Integer> myList;
    // private int myMax;
    // private int myMin;
    private int myLength;
    private final int myXRight = 500;
    private final int myXLeft = 50;
    private final int myYBottom = 500;
    private final int myYTop = 50;
    private final int myMainPanelSize = 550;
    private final int myFrameSize = 700;
    private int myIncrement;

    /**
     * Sets up state variables for line graph.
     * 
     * @param list
     */
    private void initiateLineGraph (List<Integer> list) {
        this.myList = list;
        // myMax = Collections.max(list);
        // myMin = Collections.min(list);
        myLength = list.size();
        myIncrement = (myXRight - myXLeft) / myLength;
    }

    /**
     * Paint method.
     * @param g graphics2d object
     */
    public void paint (Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        drawAxes(g2d);
        drawGraph(g2d);
    }

    private void drawAxes (Graphics2D g) {
        g.drawLine(myXLeft, myYTop, myXLeft, myYBottom);
        g.drawString("x", myXRight, myYBottom + OFFSET);
        g.drawLine(myXLeft, myYBottom, myXRight, myYBottom);
        g.drawString("y", myXLeft - OFFSET, myYTop);
        g.drawString("0", myXLeft - OFFSET, myYBottom + OFFSET);

    }

    /**
     * Draws the line graph.
     * 
     * @param g
     */
    private void drawGraph (Graphics2D g) {
        int x = myXLeft;
        for (int i = 0; i < myList.size() - 1; i++) {
            g.drawLine(x, myYBottom - myList.get(i), x + myIncrement, myYBottom -
                    myList.get(i + 1));
            x += myIncrement;
        }
    }

    /**
     * Create line graph.
     * @param scores list of scores
     * @param name name of the graph
     */
    public void createGraph (List<Integer> scores, String name) {

        LineGraphDrawer mainPanel = new LineGraphDrawer();
        mainPanel.initiateLineGraph(scores);
        JFrame frame = new JFrame(name);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainPanel.setSize(myMainPanelSize, myMainPanelSize);
        frame.setSize(myFrameSize, myFrameSize);
        frame.getContentPane().add(mainPanel);
        // frame.pack();
        // frame.setLocationByPlatform(true);
        frame.setVisible(true);
        // return mainPanel;
    }

    /**
     * Main method for the function.
     * @param args arguments to pass in
     */

   /**
    public static void main (String[] args) {
        List<Integer> scores = new ArrayList<Integer>();
        for (int i = 0; i < MAX_SCORES; i++) {
            scores.add(i);
        }
        new LineGraphDrawer().createGraph(scores, "wula");
    }
    */ 

}
